<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * relationship CRD model class 
 * 
 * 
 * NO UPDATE FOR RELATIONSHIPS!
 *
 * @author Gabor Bota <gab.bota at 3pixel.hu>
 */
Class Relationship_model extends CI_Model {

    public $relationship_table;

    function __construct() {
        // Call the Model constructor
        parent::__construct();
        $this->relationship_table = $this->db->dbprefix('term_taxonomy_realtionships');
    }

    /**
     * 
     * 
     * 
     * Delete relationship
     * 
     * 
     * 
     * @param type $id
     */
    function delete($relationship_data = array()) {
        $selected = 0;
        // delete relationship data from relationship_table
        if (isset($relationship_data['term_table_name']) && isset($relationship_data['term_id'])) {
            $this->db->where('term_table_name', $relationship_data['term_table_name']);
            $this->db->where('term_id', $relationship_data['term_id']);
            $selected++;
        }
        if (isset($relationship_data['taxonomy_table_name']) && isset($relationship_data['taxonomy_id'])) {
            $this->db->where('taxonomy_table_name', $relationship_data['taxonomy_table_name']);
            $this->db->where('taxonomy_id', $relationship_data['taxonomy_id']);
            $selected++;
        }
        if ($selected != 0) {
            $this->db->delete($this->relationship_table);
        }
    }

    /**
     * 
     * Add new relationship
     * 
     * @param type $relationship_data
     */
    function add_relationship($relationship_data = array()) {
        // if relationship not exists
        if (get_relationship($relationship_data) == NULL) {
            // insert relationship_data
            $this->db->insert($this->relationship_table, $relationship_data);
        } else {
            // relationship exists, do nothing
        }
    }

    /**
     * 
     * returns the relationship (for pagination)
     * 
     * options:
     *  select 
     *  search
     *  order_by
     *  order_dir
     *  limit
     *  offset
     * 
     * @param type $limit the number of returned relationship
     * @param type $offset from this number.
     * @return type 
     */
    function get_relationships($options = array()) {

        if (isset($options['select'])) {
            $this->db->select($options['select']);
        }

        if (isset($options['search_like'])) {
            $this->db->like('relationship_title', $options['search_like']);
            $this->db->or_like('relationship_or', $options['search_like']);
        }

        if (isset($options['limit']) && isset($options['offset']))
            $this->db->limit($options['limit'], $options['offset']);

        if (isset($options['order_by'])) {
            if (isset($options['order_dir'])) {
                $this->db->order_by($options['order_by'], $options['order_dir']);
            } else {
                $this->db->order_by($options['order_by']);
            }
        }

        $query = $this->db->get($this->relationship_table);


        return $query->result();
    }

    /**
     * 
     * Returns the number of relationship
     * 
     * 
     * @return type 
     */
    function count() {
        return $this->db->count_all_results($this->relationship_table);
    }

    /**
     * Gives back relationship by id 
     * 
     * @param type $id
     * @return array (relationship_id, relationshipname, relationship)
     */
    function get_relationship($relationship_data = array()) {
        // constructing active record query
        $this->db->where('term_table_name', $relationship_data['term_table_name']);
        $this->db->where('term_id', $relationship_data['term_id']);
        $this->db->where('taxonomy_table_name', $relationship_data['taxonomy_table_name']);
        $this->db->where('taxonomy_id', $relationship_data['taxonomy_id']);

        // do the query
        $result = $query->result();
        if ($result == NULL)
            return NULL;
        return $result[0];
    }

}